package cn.joyconn.cms.views.directive.cms;

// Generated 2015-5-10 17:54:56 by com.publiccms.common.source.SourceGenerator

import cn.joyconn.cms.common.base.AbstractTemplateDirective;
import cn.joyconn.cms.entities.cms.CmsContent;
import cn.joyconn.cms.entities.cms.CmsContentAttribute;
import cn.joyconn.cms.entities.sys.SysSite;
import cn.joyconn.cms.logic.service.cms.CmsContentAttributeService;
import cn.joyconn.cms.logic.service.cms.CmsContentService;
import com.joyconn.cms.common.handler.RenderHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static cn.joyconn.cms.common.tools.ExtendUtils.getExtendMap;
import static com.joyconn.cms.common.tools.CommonUtils.notEmpty;

/**
 *
 * CmsContentDirective
 * 
 */
@Component
public class CmsContentAndAttributeDirective extends AbstractTemplateDirective {

    @Override
    public void execute(RenderHandler handler) throws IOException, Exception {
        Long id = handler.getLong("id");
        SysSite site = getSite(handler);
        if (notEmpty(id)) {
            CmsContent entity = service.getEntity(id);
            if (null != entity && site.getId() == entity.getSiteId()) {
                handler.put("content", entity);
            }
            CmsContentAttribute cmsContentAttribute = cmsContentAttributeService.getEntity(id);
            if (null != entity) {
                Map<String, String> map = getExtendMap(cmsContentAttribute.getData());
                map.put("text", cmsContentAttribute.getText());
                map.put("source", cmsContentAttribute.getSource());
                map.put("sourceUrl", cmsContentAttribute.getSourceUrl());
                map.put("wordCount", String.valueOf(cmsContentAttribute.getWordCount()));
                handler.put("attrs", map);
            }
        }

        handler.render();
    }

    @Autowired
    private CmsContentService service;
    @Autowired
    private CmsContentAttributeService cmsContentAttributeService;

}
